package com.it.jzs.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.it.jzs.service.BlogService;
import com.it.jzs.service.BlogUserThumbsService;
import com.it.jzs.service.admin.AdminBlogService;
import com.it.jzs.vo.Result;
import com.it.jzs.vo.params.UpdateBlogParams;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("blog")
@CrossOrigin
public class BlogController {

    @Resource
    private BlogService blogService;

    @Resource
    private AdminBlogService adminBlogService;

    @Value("${file.ip}")
   private String ip;
    @Resource
    private BlogUserThumbsService blogUserThumbsService;

    @GetMapping("/type/{id}")// 博客分类列表页查询
    public Result<Object> selectBlogTypeList(@PathVariable("id") Integer id) {
        Result<Object> result = blogService.selectBlogTypeList(id);
        System.out.println("ip = " + ip);
        return result;

    }

    @GetMapping("/tag/{id}")// 博客标签列表页查询
    public Result<Object> selectBlogTagList(@PathVariable("id") Integer id) {
//        System.out.println("id = " + id);
        Result<Object> result = blogService.selectBlogTagList(id);
        return result;

    }

    @GetMapping("/tag/name/{id}")// 博客分类列表页查询
    public Result<Object> selectTagName(@PathVariable("id") Integer id) {
//        System.out.println("id = " + id);
        Result<Object> result = blogService.selectTagName(id);
        return result;

    }

    @GetMapping("/index/list")// 首页博客列表数据
    public Result<Object> selectBlogList(@RequestParam("index") Integer index, @RequestParam("size") Integer size, @RequestParam("num") Integer num) {
        Result<Object> result = blogService.selectBlogList(index, size, num);
        return result;
    }

    @GetMapping("/index/latestblog")  // 首页最新博客
    public Result<Object> selectLatestBlog() {
        Result<Object> result = blogService.selectLatestBlog();
        return result;
    }

    @GetMapping("/index/countblog")  // 首页归档数据
    public Result<Object> selectCountBlog() {
        Result<Object> result = blogService.selectCountBlog();
        return result;

    }

    @GetMapping("/index/thumbscount/{id}") // 首页 收到的赞数量
    public Result<Object> selectThumbsCount(@PathVariable("id") Integer id) {
        Result<Object> result = blogUserThumbsService.selectThumbsCount(id);

        return result;

    }

    @GetMapping("/index/custom")   // 首页自定义数据
    public Result<Object> selectCustom() {
        Result<Object> result = blogService.selectCustom();
        return result;
    }

    @GetMapping("/index/swiper")   // 首页轮播图
    public Result<Object> selectSwiper() {
        Result<Object> result = blogService.selectSwiper();
        return result;
    }

    @GetMapping("/index/search")   // 首页模糊查询
    public Result<Object> selectBlogByKeyWords(@RequestParam("keyWords") String
                                                       keyWords, @RequestParam("index") Integer index, @RequestParam("size") Integer size, @RequestParam("num") Integer
                                                       num) {
//        System.out.println("keyWords = " + keyWords);
//        System.out.println("index = " + index);
//        System.out.println("size = " + size);
//        System.out.println("num = " + num);
        Result<Object> result = blogService.selectBlogByKeyWords(keyWords, index, size, num);
//        return Result.success(null, null);
        return result;
    }

    @GetMapping("/archive/{time}")  // 归档数据列表
    public Result<Object> selectArchive(@PathVariable("time") String time) {
//        System.out.println("time = " + time);
        Result<Object> result = blogService.selectArchiveByTime(time);
//        return result;
        return Result.success(result, "获取对应的归档数据成功");
    }

    @GetMapping("/detail/{id}")  // 博客详情页
    public Result<Object> selectBlogDetail(@PathVariable("id") Integer id) {
        Result<Object> result = blogService.selectBlogDetail(id);
        return result;
    }

    @PostMapping("/detail/thumbs/{id}/{userId}")  // 给博客点赞
    public Result<Object> updateThubms(@PathVariable("id") Integer id, @PathVariable("userId") Integer userId) {
//        System.out.println("id = " + id);
//        System.out.println("userId = " + userId);
        Result<Object> result = blogService.updateThubms(id, userId);
        return result;
    }

    @PostMapping("/uploadvideo")
    public JSON uploadVideo(MultipartFile file) throws IOException { // 分类图片上传接口
        String originalFilename = file.getOriginalFilename();// 获取上传的文件名
        int i = originalFilename.lastIndexOf("."); // 查看最后一个出现.的位置
        String houzui = originalFilename.substring(i);  // 截取 这个索引开始也就是.开始的后缀
        UUID uuid = UUID.randomUUID();
//        System.out.println("uuid.toString() = " + uuid.toString());
        StringBuilder builder = new StringBuilder();
        builder.append(uuid).append(houzui);  // 减少因为字符串的拼接消耗的性能
        String rootFilePath = "E:\\upload02\\" + builder.toString();
        String url = "http://localhost:8888/upload02/" + builder.toString();
//        System.out.println("url = " + url);
        File file1 = FileUtil.writeBytes(file.getBytes(), rootFilePath);

//        File file1 = FileUtil.writeBytes(file.getBytes(), rootFilePath);

//        System.out.println("originalFilename = " + originalFilename);
        JSONObject json = new JSONObject();
        json.set("errno", 0);
        JSONObject data = new JSONObject();
        data.set("url", url);
        json.set("data", data);
        return json;
    }

    @PostMapping("/upload")   // 上传图片
    public JSON upload(MultipartFile file) throws IOException { // 分类图片上传接口

        String originalFilename = file.getOriginalFilename();// 获取上传的文件名
        int i = originalFilename.lastIndexOf("."); // 查看最后一个出现.的位置
        String houzui = originalFilename.substring(i);  // 截取 这个索引开始也就是.开始的后缀
        UUID uuid = UUID.randomUUID();
//        System.out.println("uuid.toString() = " + uuid.toString());
        StringBuilder builder = new StringBuilder();
        builder.append(uuid).append(houzui);  // 减少因为字符串的拼接消耗的性能
        String rootFilePath = "E:\\upload01\\" + builder.toString();
        String url = "http://localhost:8888/upload01/" + builder.toString();
//        System.out.println("url = " + url);
        File file1 = FileUtil.writeBytes(file.getBytes(), rootFilePath);

//        System.out.println("originalFilename = " + originalFilename);
        JSONObject json = new JSONObject();
        json.set("errno", 0);
        JSONArray arr = new JSONArray();
        JSONObject data = new JSONObject();
        arr.add(data);
        data.set("url", url);
        json.set("data", arr);
        return json;
    }

    @GetMapping("/platform/{id}")   // 用户个人中心博客  用户id
    public Result<Object> selectPlatform(@PathVariable("id") Integer id) {
        Result<Object> result = blogService.selectPlatform(id);
        return result;
    }

    @DeleteMapping("/platform/{id}/{userId}")   // 用户个人中心博客  根据博客id 删除博客 只能删除本用户的博客
    public Result<Object> deleteBlog(@PathVariable("id") Integer id, @PathVariable("userId") Integer userId) {
        Result<Object> result = blogService.deleteBlog(id, userId);
        return result;
    }

    @PutMapping("/platform")
    public Result<Object> updateBlogPlatform(@RequestBody UpdateBlogParams updateBlogParams) { // 普通用户的修改，要加些验证
        System.out.println("updateBlogParams = " + updateBlogParams);
//        return Result.success(null, "测试");
        Result<Object> result = adminBlogService.updateBlogPlatform(updateBlogParams);
        return result;
    }
}